package com.fulei.logic;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class EastUpsOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int colseTimeCmd(String upsData)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = sendColseUpsData(upsData);
      



      LogUtil.writeEleLockLog("EastColseTimeCmd:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd)
      {
        serialPort.writeBytes(SendData);
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("colseTimeCmd关机异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int restTimeCmd(String upsData)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = sendRestUpsData(upsData);
      



      LogUtil.writeEleLockLog("EastRestTimeCmd:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd)
      {
        serialPort.writeBytes(SendData);
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("restTimeCmd关机重启异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int clearCmd()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[2];
      SendData[0] = 67;
      SendData[1] = 13;
      
      LogUtil.writeEleLockLog("EastClearCmd:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd)
      {
        serialPort.writeBytes(SendData);
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("clearCmd清除定时开关机指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte[] sendColseUpsData(String timeData)
  {
    byte[] SendData = new byte[4];
    try
    {
      int oneData = Integer.parseInt(timeData.substring(0, 1));
      int twoData = Integer.parseInt(timeData.substring(1, 2));
      SendData[0] = 83;
      SendData[1] = ((byte)((byte)oneData + 48));
      SendData[2] = ((byte)((byte)twoData + 48));
      SendData[3] = 13;
    }
    catch (Exception localException) {}
    return SendData;
  }
  
  public static byte[] sendRestUpsData(String timeData)
  {
    byte[] SendData = new byte[9];
    try
    {
      int oneCloseData = Integer.parseInt(timeData.substring(0, 1));
      int twoCloseData = Integer.parseInt(timeData.substring(1, 2));
      
      int oneOpenData = Integer.parseInt(timeData.substring(3, 4));
      int twoOpenData = Integer.parseInt(timeData.substring(4, 5));
      int threeOpenData = Integer.parseInt(timeData.substring(5, 6));
      int fourOpenData = Integer.parseInt(timeData.substring(6, 7));
      SendData[0] = 83;
      SendData[1] = ((byte)((byte)oneCloseData + 48));
      SendData[2] = ((byte)((byte)twoCloseData + 48));
      SendData[3] = 82;
      SendData[4] = ((byte)((byte)oneOpenData + 48));
      SendData[5] = ((byte)((byte)twoOpenData + 48));
      SendData[6] = ((byte)((byte)threeOpenData + 48));
      SendData[7] = ((byte)((byte)fourOpenData + 48));
      SendData[8] = 13;
    }
    catch (Exception localException) {}
    return SendData;
  }
}
